import hashlib
import sys
import time
import re
# 操作html界面的
from selenium.webdriver.common.by import By

from automate_testing.operation.bankcard_operate import get_userId
from automate_testing.tool.DButils import DButils
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def get_banlance(username, cardNum):
    if not username or not cardNum:
        return -1
    userId = get_userId(username)
    select_banlance_sql = 'select  balance from bankcard where userId=%s and cardNum=%s'
    data = DButils().select(select_banlance_sql, (userId, cardNum))
    return data[0][0]
class InvestOperate:
    def enter_invest_page(func):
        def wrapper(*args, **kwargs):
            # 进入理财界面


            # 调用原始函数
            result = func(*args, **kwargs)
            return result

        return wrapper
    @staticmethod
    def to_change_money(driver,username,cardNum,paypwd,in_paypwd):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        driver.find_element(By.XPATH,'//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
        time.sleep(1)
        # driver.get('http://localhost:90/user/finance/toChangeMoney.html')
        # driver.refresh()

        money = driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[5]').text
        money = round(float(money.replace('元', '')),2)
        driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button').click()
        time.sleep(1)

        # 该模态框需要获得焦点
        input_password=driver.find_elements(By.XPATH, '//*[@id="password"]')[1]
        input_password.click()
        input_password.send_keys(str(in_paypwd))

        confirm_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]'))
        )
        confirm_button.click()
        time.sleep(1)
        success_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button'))
        )
        success_button.click()

        banlance=get_banlance(username,cardNum)
        if paypwd!=0 and in_paypwd==paypwd and banlance>=money:
            return money
        else:
            return 0

    @staticmethod
    def to_term_financial(driver, username, cardNum, paypwd, in_paypwd):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]/a').click()
        time.sleep(1)

        money = driver.find_element(By.XPATH,
                                    '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[5]').text
        money = round(float(money.replace('元', '')), 2)
        driver.find_element(By.XPATH,
                            '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button').click()
        time.sleep(1)

        # 该模态框需要获得焦点
        input_password = driver.find_elements(By.XPATH, '//*[@id="password"]')[1]
        input_password.click()
        input_password.send_keys(str(in_paypwd))

        confirm_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]'))
        )
        confirm_button.click()
        time.sleep(1)
        success_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button'))
        )
        success_button.click()

        banlance = get_banlance(username, cardNum)
        if paypwd != 0 and in_paypwd == paypwd and banlance >= money:
            return money
        else:
            return 0

    @staticmethod
    def to_fund_product(driver, username, cardNum, paypwd, in_paypwd):
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
        time.sleep(1)
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a').click()
        time.sleep(1)

        money = driver.find_element(By.XPATH,
                                    '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]').text
        money = round(float(money.replace('元', '')), 2)
        driver.find_element(By.XPATH,
                            '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button').click()
        time.sleep(1)

        # 该模态框需要获得焦点
        input_password = driver.find_elements(By.XPATH, '//*[@id="password"]')[1]
        input_password.click()
        input_password.send_keys(str(in_paypwd))

        confirm_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="layui-layer1"]/div[3]/a[1]'))
        )
        confirm_button.click()
        time.sleep(1)
        success_button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.XPATH, '/html/body/div[3]/div[2]/div/div/div/div/div/div/div/div[4]/button'))
        )
        success_button.click()
        time.sleep(3)
        banlance = get_banlance(username, cardNum)
        if paypwd != 0 and in_paypwd == paypwd and banlance >= money:
            return money
        else:
            return 0

if __name__ == '__main__':
    res= InvestOperate.get_banlance('lisi','1234567891548796')
    print(res)